package command;

import model.EnhancedCostEvaluator;
import model.IncrementalCostCalculator;
import model.IncrementalValidator;
import model.Solution;
import model.SolverEnvironment;

public class IncrementalMoveProcessToMachineCommand implements Command {

	IncrementalValidator validator;
	IncrementalCostCalculator costEvaluator;
	Solution potential_solution;
	int p_id;
	int m_id;
	int old_m_id = -1;
//	long[] costComponents;

	public IncrementalMoveProcessToMachineCommand(IncrementalValidator iv, IncrementalCostCalculator icc) {
		this.costEvaluator = icc;
		this.validator = iv;
		this.potential_solution = validator.getCurrentSolution();
	}
	
	public IncrementalMoveProcessToMachineCommand(SolverEnvironment env) {
		this.costEvaluator = env.getCostCalculator();
		this.validator = env.getValidator();
		this.potential_solution = env.getPotentialSolution();
	}
	
	public IncrementalMoveProcessToMachineCommand(SolverEnvironment env, int p_id, int m_id) {
		this(env);
		this.p_id = p_id;
		this.m_id = m_id;
	}

	public int getP_id() {
		return p_id;
	}

	public void setP_id(int p_id) {
		this.p_id = p_id;
	}

	public int getM_id() {
		return m_id;
	}

	public void setM_id(int m_id) {
		this.m_id = m_id;
	}

	@Override
	public void execute() {
		old_m_id = potential_solution.getMachineIdForProcessId(p_id);
		
//		potential_solution.removeProcess(p_id);
//		potential_solution.assignProcessToMachine(p_id, m_id);
//		System.out.printf("assign p%d to m%d which was in m%d\n", p_id, m_id,
//				old_m_id);
		//TODO: Integrate all structures in Solutions PA 13022012
		potential_solution.moveProcess(p_id, old_m_id, m_id);
		costEvaluator.updateServiceMove(p_id, old_m_id, m_id);
//		validator.updateServiceProcessesDistribution(p_id, old_m_id, m_id);
//		validator.updateTransientResourceUsageOfMachine(p_id, old_m_id, m_id);

	}

	@Override
	public void undo() {
//		potential_solution.removeProcess(p_id);
//		potential_solution.assignProcessToMachine(p_id, old_m_id);
//		System.out.printf("assign p%d to m%d\n", p_id, old_m_id);
		//TODO: Integrate all structures in Solutions PA 13022012
		potential_solution.moveProcess(p_id, m_id, old_m_id);
		costEvaluator.updateServiceMove(p_id, m_id, old_m_id);
//		validator.updateServiceProcessesDistribution(p_id, m_id, old_m_id);
//		validator.updateTransientResourceUsageOfMachine(p_id, m_id, old_m_id);
		old_m_id = -1;
	}

	@Override
	public String toString() {
		return "IncrementalMoveProcessToMachineCommand [m_id=" + m_id + ", old_m_id=" + old_m_id + ", p_id=" + p_id + "]";
	}
}
